
package com.chasekernan.hxnova.client.plugins;

#if flash9

import com.chasekernan.hxnova.core.players.Player;
import flash.display.Loader;
import flash.events.Event;
import flash.net.URLRequest;
import flash.system.ApplicationDomain;
import flash.system.LoaderContext;

import com.chasekernan.hxnova.client.ClientBase;

class PluginApi {
    
    private static var INSTANCE         : PluginApi;
    
    public var currentObject            : Dynamic;
    public var player                   : Player;
    
    private var rightClickListeners     : List<Void -> Void>;
    
    public static function getInstance() : PluginApi {
        if (INSTANCE == null) INSTANCE = new PluginApi();
        
        return INSTANCE;
    }
    
    
    private function new() {
        currentObject       = flash.Lib.current;
        rightClickListeners = new List();
        
        PluginManager.getInstance().addPluginVariable("PluginApi", this);
        ClientBase.getInstance().addCommunicationObject("PluginApi", this);
    }
    
    public function createPlugin(name : String, author = "Unspecified", 
                                 description = "", version = 1.0) : Plugin {
        return {name        : name, 
                author      : author, 
                description : description, 
                version     : version, 
                setup       : function() {}, 
                tearDown    : function() {},
               };
    }
    
    public function loadSwf(path : String, onComplete : Void -> Void) {
        var loader  = new Loader();
        loader.contentLoaderInfo.addEventListener(Event.COMPLETE, 
            function(_) { 
                onComplete(); 
            }
        );
                
        loader.load(new URLRequest(path), 
                    new LoaderContext(false, ApplicationDomain.currentDomain));
    }
    
    public function addRightClickListener(listener : Void -> Void) {
        rightClickListeners.add(listener);
    }
    
    public function removeRightClickListener(listener : Void -> Void) {
        rightClickListeners.remove(listener);
    }
    
    public function onRightClick() {
        for (listener in rightClickListeners) listener();
    }
}

#end